//
// OpenTemplate
// Copyright (c) 2006
// by OpenArrow Software ( http://www.openarrow.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using OpenArrow.Templating.Properties;

namespace OpenArrow.Templating
{
    /// <summary>
    /// Represents a collection of <see cref="Problem"/> objects
    /// </summary>
    [Serializable()]
    public class ProblemCollection : Collection<Problem>
    {
        private int warningCount;
        private int errorCount;

        /// <summary>
        /// Gets the number of warnings in the collection
        /// </summary>
        public int WarningCount
        {
            get { return warningCount; }
        }

        /// <summary>
        /// Gets the number of errors in the collection
        /// </summary>
        public int ErrorCount
        {
            get { return errorCount; }
        }

        /// <summary>
        /// Inserts an item into the collection
        /// </summary>
        /// <param name="index">The index of the item to insert</param>
        /// <param name="item">The item to insert</param>
        /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="index"/> is less than zero</exception>
        /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="index"/> is greater than <see cref="Collection{T}.Count"/></exception>
        protected override void InsertItem(int index, Problem item)
        {
            base.InsertItem(index, item);

            if (item.IsError)
                errorCount++;
            else
                warningCount++;
        }

        /// <summary>
        /// Removes an item from the collection
        /// </summary>
        /// <param name="index">The index of the item to remove</param>
        /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="index"/> is less than zero</exception>
        /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="index"/> is greater than <see cref="Collection{T}.Count"/></exception>
        protected override void RemoveItem(int index)
        {
            if (base[index].IsError)
                errorCount--;
            else
                warningCount--;

            base.RemoveItem(index);
        }

        /// <summary>
        /// Clears the collections of all items
        /// </summary>
        protected override void ClearItems()
        {
            base.ClearItems();
            errorCount = 0;
            warningCount = 0;
        }

        /// <summary>
        /// Adds multiple items to the collection
        /// </summary>
        /// <param name="problems">The items to add</param>
        /// <exception cref="ArgumentNullException"><paramref name="problems"/> is <see langword="null"/></exception>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object)")]
        public void AddRange(IEnumerable<Problem> problems)
        {
            if (problems == null)
                throw new ArgumentNullException(String.Format(Resources.Exception_ArgIsNullOrEmpty, "problems"), "problems");

            foreach (Problem prob in problems)
                base.Add(prob);
        }

        /// <summary>
        /// Throws a <see cref="ActionProblemsException"/> if there is at least one error in the collection.
        /// </summary>
        /// <exception cref="OpenArrow.Templating.ActionProblemsException"><see cref="ErrorCount"/> is greater than 0</exception>
        public void Throw() { if (this.errorCount > 0) throw new ActionProblemsException(this); }
    }
}
